package com.firefly.dao.assembler.ums;

import com.firefly.dao.dao.ums.po.AdminPo;
import com.firefly.dao.dto.ums.AdminCreateDto;
import com.firefly.dao.dto.ums.AdminUpdateDto;
import com.firefly.dao.dto.ums.AdminUpdateSelfDto;
import com.firefly.dao.vo.ums.AdminNameVo;
import com.firefly.dao.vo.ums.AdminVo;
import com.firefly.util.utils.FuncUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 管理员Model转换
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/9
 **/
@Component
public class AdminAssembler {
    public List<AdminVo> getList(List<AdminPo> list){
        List<AdminVo> result = new ArrayList<>();
        list.forEach(item -> {
            AdminVo tmp = new AdminVo();
            tmp.setId(item.getId());
            tmp.setName(item.getName());
            tmp.setStatus(item.getStatus());
            result.add(tmp);
        });
        return result;
    };
    public List<AdminNameVo> getNameList(List<AdminPo> list){
        List<AdminNameVo> result = new ArrayList<>();
        list.forEach(item -> {
            AdminNameVo tmp = new AdminNameVo();
            tmp.setId(item.getId());
            tmp.setName(item.getName());
            tmp.setRealname(item.getRealname());
            tmp.setPhone(item.getPhone());
            result.add(tmp);
        });
        return result;
    }

    public AdminPo getPo(AdminCreateDto detail){
        AdminPo entity = new AdminPo();
        entity.setName(detail.getName());
        entity.setRealname(detail.getRealname());
        entity.setPhone(detail.getPhone());
        entity.setEmail(detail.getEmail());
        entity.setStatus(detail.getStatus());
        // 加密，秘钥
        Map<String,String> pwdMap = FuncUtils.getPassword(detail.getPassword(),10);
        entity.setCrypt(pwdMap.get("crypt"));
        entity.setPassword(pwdMap.get("pwd"));
        // ip
        entity.setLastip("0.0.0.0");
        // 取时间
        entity.setLasttime(LocalDateTime.now());
        return entity;
    }
    public AdminPo getPo(AdminUpdateDto detail){
        AdminPo entity = new AdminPo();
        entity.setId(detail.getId());
        entity.setRealname(detail.getRealname());
        entity.setPhone(detail.getPhone());
        entity.setEmail(detail.getEmail());
        entity.setStatus(detail.getStatus());
        return entity;
    }
    public AdminPo getPo(int adminId, AdminUpdateSelfDto detail){
        AdminPo entity = new AdminPo();
        entity.setId(adminId);
        entity.setRealname(detail.getRealname());
        entity.setPhone(detail.getPhone());
        entity.setEmail(detail.getEmail());
        return entity;
    }
}
